<?php
/**
 * Created by PhpStorm.
 * User: sunjian
 * Date: 2018/3/11
 * Time: 13:35
 */

namespace frontend\controllers;

use app\models\Customer;
use app\models\ExchangeCate;
use app\models\ExchangeItem;
use app\models\ExchangeOrder;
use app\models\ScoreLog;
use Yii;

class CustomerController extends BaseController
{
    public $layout="customer";
    public function actionLogin(){
        $this->layout="login";
        Yii::$app->user->logout();
        return $this->render("login");
    }
    public function actionSign(){
        $request = Yii::$app->request;
        $return = [];
        if($request->isAjax){
            $username = $request->post("username");
            $password = $request->post("password");
            $user = Customer::findOne(['username'=>$username]);
            if($user){
                if(Yii::$app->security->validatePassword($password,$user->password)){
                    //Yii::$app->user->login($user);
                    Yii::$app->session->set("cid",$user->id);
                    $return['status']=true;

                }else{
                    $return['status']=false;
                    $return['msg']="用户名或密码错误";

                }
            }else{
                $return['status']=false;
                $return['msg']="用户名或密码错误";


            }
        }
        return json_encode($return);
    }

    public function actionIndex(){

        $request = Yii::$app->request;
        $cates = ExchangeCate::find()->where(['flag'=>1])->asArray()->all();
        $cate = $request->post("cate")?$request->post("cate"):$cates[0]['id'];
        $items = ExchangeItem::find()->where(['cate_id'=>$cate,'flag'=>1])->asArray()->all();
        return $this->render("index",['cates'=>$cates,'items'=>$items,'now'=>$cate]);
    }

    public function actionOrder(){
        $order = ExchangeOrder::find()->where(['c_id'=>$c_id = Yii::$app->session['cid']])->asArray()->all();
        return $this->render("order",['orders'=>$order]);
    }

    public function actionData(){
        $c_id = Yii::$app->session['cid'];
        $user = Customer::findOne($c_id);
        $green = ScoreLog::find()->where("c_id=$c_id and flag!=0 and type=2")->sum("score");
        $blue = ScoreLog::find()->where("c_id=$c_id and flag!=0 and type=1")->sum("score");

        return $this->render("data",['user'=>$user,'blue'=>$blue,'green'=>$green]);
    }

    public function actionLog(){
        $c_id = Yii::$app->session['cid'];
        $log = ScoreLog::find()->where("c_id=$c_id and flag!=0")->asArray()->all();
        return $this->render("log",['logs'=>$log]);
    }

    public function actionInfo(){
        $c_id = Yii::$app->session['cid'];
        $user = Customer::findOne($c_id);
        return $this->render("info",['user'=>$user]);
    }
    public function actionPay(){
        $request = Yii::$app->request;
        if($request->isAjax){
            $id=$request->post("id");
            $item = ExchangeItem::findOne($id);
            $type = $item->type==1?"blue":"green";
            $c_id = Yii::$app->session['cid'];
            $user = Customer::findOne($c_id);

            $return = [];
            if($user->$type<$item->price){
                $return['status']=false;
                $return['msg']="积分不足";
                return  json_encode($return);
            }
            $tr= Yii::$app->db->beginTransaction();
            try{
                $order = new ExchangeOrder();
                $order->c_id=$c_id;
                $order->orderId=time().rand(0000,9999);
                $order->title = $item->title;
                $order->type  = $item->type;
                $order->price = $item->price;
                $order->create_time= time();
                if($order->save()){
                    //减库存
                    $item->num = $item->num-1;
                    if($item->save()){
                        //记录日志
                        $log = new ScoreLog();
                        $log->c_id = $c_id;
                        $log->type = $item->type;
                        $log->ex_id = $order->id;
                        $log->score = $item->price;
                        $log->des=$item->title;
                        $log->create_time = time();
                        if($log->save()){
                            $tr->commit();
                            $return['status']=true;
                            return json_encode($return);
                        }else{
                            $tr->rollBack();
                            $return['status']=false;
                            $return['msg']="积分日志失败".json_encode($log->errors);
                            return json_encode($return);
                        }
                    }else{
                        $tr->rollBack();
                        $return['status']=false;
                        $return['msg']="库存操作失败".json_encode($item->errors);
                        return json_encode($return);
                    }
                }else{
                    $tr->rollBack();
                    $return['status']=false;
                    $return['msg']="订单生成失败".json_encode($order->errors);
                    return json_encode($return);
                }

            }catch (Exception $e){
                $tr->rollBack();
                return $this->render("error");
            }
        }
    }
}